// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

// 全局配置
import config from './config.js'
Vue.prototype.CFG = config



// 自定义字体
import './assets/fonts/font.css';

// Cookies
import cookies from './utils/cookies.js'

router.beforeEach((to, from, next) => {

  //进入登录页面的时候清除 token
  if (to.path === '/login') {
    cookies.setCookie('token', '')
  }

  store.state.token = sessionStorage.getItem('token'); //获取本地存储的token

  if (to.meta.requireAuth) { // 判断该路由是否需要登录权限

    let token = cookies.getCookie('token');

    if (token !== "" && token !== null) { // 通过vuex state获取当前的token是否存在

      //判断是否需要缓存
      if (to.path === '/home' || from.path === '/category') {
        to.meta.keepAlive = true; // 让 列表页 缓存，即不刷新
        next();
      } else {
        to.meta.keepAlive = false; // 让 列表页 即不缓存，刷新
        next();
      }

    } else {
      next({
        path: '/login',
        query: { redirect: from.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }

  } else {
    next();
  }
})

import MyInputNumber from './components/my-input-number'
Vue.use(MyInputNumber)

// 引入第三方UI库
import './ui/vant-ui.js'

// 发送数据请求
import http from './utils/http.js'
Vue.use(http)

// 引入第三方图标库
import '@/assets/iconfont/iconfont.css';

//
import store from './store/index.js'

window.OPENID_URL = config.wxLoginUrl;

Vue.config.productionTip = false


//window.eventBus = new Vue()
Vue.prototype.$EventBus = new Vue()

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
